package com.xihuanjun.healthcode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xihuanjun.healthcode.dao.UserDao;
import com.xihuanjun.healthcode.entity.User;
import com.xihuanjun.healthcode.entity.convert.UserConvert;
import com.xihuanjun.healthcode.entity.dto.UserDTO;
import com.xihuanjun.healthcode.entity.vo.UserVO;
import com.xihuanjun.healthcode.exception.ServiceException;
import com.xihuanjun.healthcode.service.UserService;
import com.xihuanjun.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2022-10-25 09:43:22
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    UserConvert userConvert;

    /**
     * 用户登录
     *
     * @param loginName
     * @param pwd
     * @return
     */
    @Override
    public UserVO login(String loginName, String pwd) {
        LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(User.class)
                .eq(User::getLoginName, loginName);
        User user = this.getOne(wrapper);
        if (user == null) {
            throw new ServiceException("没有这个账号");
        }
        if (MD5Utils.verify(pwd, user.getPwd())) {
            return userConvert.user2VO(user);
        }
        throw new ServiceException("密码不正确");
    }

    /**
     * 修改资料
     *
     * @return
     */
    @Override
    public boolean updateInfo(UserDTO userDTO) {
        LambdaQueryWrapper wrapper=Wrappers.lambdaQuery(User.class)
                .eq(User::getId,userDTO.getId());
        User user=new User();
        user.setContactName(userDTO.getContactName());
        user.setPhone(userDTO.getPhone());
        this.update(user,wrapper);
        return true;
    }

    /**
     * 修改密码
     *
     * @param id
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @Override
    public boolean updatePwd(long id, String oldPwd, String newPwd) {
        LambdaQueryWrapper wrapper=Wrappers.lambdaQuery(User.class)
                .eq(User::getId,id);
        User user = this.getOne(wrapper);
        if (MD5Utils.verify(oldPwd, user.getPwd())) {
            user.setPwd(MD5Utils.md5(newPwd));
            this.updateById(user);
            return true;
        }else{
            throw new ServiceException("原密码不正确");
        }
    }

    @Override
    public UserVO get(long id) {
        LambdaQueryWrapper wrapper=Wrappers.lambdaQuery(User.class)
                .eq(User::getId,id);
        User user = this.getOne(wrapper);
        if(user!=null){
            return userConvert.user2VO(user);
        }
        return null;
    }


    /**
     * 获取除admin之外的所有用户
     *
     * @return
     */
    @Override
    public List<UserVO> subordinate() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.select("id","name");//只查id和name列
        queryWrapper.ne("login_name","admin");//不查名字是admin的
        queryWrapper.orderBy(true,true,"convert(`name` using gbk)");//按名字排序
        List<User> list = this.list(queryWrapper);
        List<UserVO> collect = list.stream().map(userConvert::user2VO).collect(Collectors.toList());
        return collect;
    }
}

